package com.huadi.firematerialident.controller;


import ch.qos.logback.core.net.SyslogOutputStream;
import com.huadi.firematerialident.po.Application;
import com.huadi.firematerialident.po.FireInfo;
import com.huadi.firematerialident.po.FireMaterialEvidenceInfo;
import com.huadi.firematerialident.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import java.util.*;

@Controller
@RequestMapping("/app")
public class ApplicationController {

    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    ApplicationServiceImpl applicationService;
    @Autowired
    FireMaterialEvidenceInfoServiceImpl fireMaterialEvidenceInfoService;
    @Autowired
    FireInfoServiceImpl fireInfoService;
    @Autowired
    NormalUserServiceImpl normalUserService;
    @Autowired
    UserServiceImpl userService;

    @RequestMapping("/appCommit")
    public String commitApplication(@RequestParam("MEID") String MEID,
                                    @RequestParam("InspectionUnitName") String InspectionUnitName,
                                    @RequestParam("InspectionUnitAdd") String InspectionUnitAdd,
                                    Model model){
        System.out.println("commitApplication started");
        String role = userService.getRole();
        if(!role.equals("normaluser")){
            model.addAttribute("msg","请以普通用户身份登录!");
            return "app/appcommit";
        }

        //判断物证数据库中是否存在该信息，如果不存在，则弹出提示


        if(fireMaterialEvidenceInfoService.JudgeMaterial(MEID) == 0){
            model.addAttribute("msg","请先将相关物证信息录入系统！");
            return "/app/committed";
        }



        //创建申请表
        Application application = new Application();
        application.setAppID();
        application.setMEID(MEID);
        application.setNUserID(normalUserService.getNUserID());
        application.setInspectionUnitName(InspectionUnitName);
        application.setInspectionUnitAdd(InspectionUnitAdd);
        applicationService.addApplication(application);

        model.addAttribute("msg","提交成功！");
        return "/app/appcommit";
    }

    @RequestMapping("/appUpdate")
    public String updateApplication(@RequestParam("AppID") String appid,
                                    @RequestParam("MEID") String meid,
                                    @RequestParam("NUserID") String userid,
                                    @RequestParam("InspectionUnitName") String iname,
                                    @RequestParam("InspectionUnitAdd") String iadd, Model model){

        //权限检测，只能修改自己的申请表
        String NUserID = normalUserService.getNUserID();
        if(!userid.equals(NUserID)){
            model.addAttribute("msg", "您没有权限修改该表");
            return "/app/committed";
        }



        Application application = applicationService.selectApplicationByID_class(appid);
        application.setMEID(meid);
        application.setNUserID(userid);
        application.setInspectionUnitName(iname);
        application.setInspectionUnitAdd(iadd);

        if(applicationService.updateApplication(application)==0){
            model.addAttribute("msg","修改失败！");
        }
        else{
            model.addAttribute("msg","修改成功！");
        }
        return "/app/committed";
    }

    @RequestMapping("/getMyApp")
    @ResponseBody
    public String getMyApp(){
        List<Map<String,Object>> list = applicationService.getMyApplication();
        System.out.println("List<Map<String,Object>>:"+list);
        return userService.ListToJSON(list.size(),list);
    }

    @RequestMapping("/getAllApp")
    @ResponseBody
    public String getAllApp(){
        List<Map<String,Object>> list = applicationService.getAllApplication();
        return userService.ListToJSON(list.size(),list);
    }


    @RequestMapping("/select")
    @ResponseBody
    public String selectApplication(String appID,Model model){
        List<Map<String,Object>> list = jdbcTemplate.queryForList("select * from application where AppID=?",appID);
        if(list.isEmpty()) {
            model.addAttribute("msg", "没有查找到该表！");
        }
        return userService.ListToJSON(list.size(),list);
    }

    @RequestMapping("/getIdentified")
    @ResponseBody
    public String getIdentified(Model model){
        List<Map<String,Object>> list = applicationService.getIdentified();
        System.out.println("List<Map<String,Object>>:"+list);
        return userService.ListToJSON(list.size(),list);
    }
}
